package com.yuejian_06.yuejian_06.model;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.bean.UserInfoBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.fragment.Constants_l;
import com.yuejian_06.yuejian_06.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by __ZL__ on 2017/9/25.
 */

public class LoginModel {

    private Retrofit retrofit;
    public LoginModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.SERVERADDRESS)//增加返回值为Gson的支持（一实体类返回）
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //登录
    public void login(String phonenum, String user_pwd, final LoginListener loginListener){
        LoginService loginService = retrofit.create(LoginService.class);
        Call<UserInfoBean> call=loginService.login(phonenum,user_pwd);
        call.enqueue(new Callback<UserInfoBean>() {
            @Override
            public void onResponse(Call<UserInfoBean> call, Response<UserInfoBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<UserInfoBean> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //注册
    public void reg(String user_name, String user_pwd,
                    String address, String phonenum,
                    final LoginListener loginListener){
        LoginService loginService = retrofit.create(LoginService.class);
        Call<SuccessBean> call=loginService.reg(user_name,user_pwd,address,phonenum);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                loginListener.onFail(t.toString());
            }
        });
    }

}
